package com.microframework.boot.visual.controller;

import java.sql.Date;
import java.util.List;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jfinal.kit.StrKit;
import com.microframework.base.core.datasource.annotation.Tx;
import com.microframework.base.core.util.SecurityUtils;
import com.microframework.base.core.util.UuidUtil;
import com.microframework.base.web.resp.R;
import com.microframework.boot.visual.model.domain.ComponentDataModel;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("/visual/component/data-model")
@Tag(name = "组件数据模型管理", description = "可视化设计器组件数据模型接口管理相关接口")
public class ComponentDataModelController {
	
	@Operation(summary = "组件对应的数据模型列表")
	@Parameters({ @Parameter(name = "componentId", description = "组件Id", required = true) })
	@GetMapping("/{componentId}/list")
	public R<List<ComponentDataModel>> list(HttpServletRequest request, @PathVariable("componentId") String componentId) {
		try {
			List<ComponentDataModel> list=ComponentDataModel.dao.find("select * from "+ComponentDataModel.TABLE_NAME+" where component_id=? order by sort asc ",componentId);
			return R.ok(list);
		} catch (Exception e) {
			return R.fail(e.getMessage());
		}
	}
	
	
	@Operation(summary = "保存组件数据模型")
	@Parameters({ 
		@Parameter(name = "componentId", description = "组件Id", required = true),
		@Parameter(name = "componentDataModel", description = "组件数据模型信息", required = true) })
	@PostMapping("/{componentId}/save")
	@Tx
	public R<ComponentDataModel> save(@PathVariable("componentId") String componentId, @RequestBody ComponentDataModel componentDataModel) {
		try {
			if (StrKit.notBlank(componentDataModel.getId())) {
				componentDataModel.setModifyTime(new Date(System.currentTimeMillis()));
				componentDataModel.setModifyUserId(SecurityUtils.getUserId());
				if (componentDataModel.update()) {
					return R.ok(componentDataModel);
				} else {
					return R.fail("更新失败！");
				}
			} else {
				componentDataModel.setId(UuidUtil.getUUID());
				componentDataModel.setCreateTime(new Date(System.currentTimeMillis()));
				componentDataModel.setCreateUserId(SecurityUtils.getUserId());
				if (componentDataModel.save()) {
					return R.ok(componentDataModel);
				} else {
					return R.fail("保存失败.");
				}
			}
		} catch (Exception e) {
			return R.fail(e.getMessage());
		}
	}
	
	@Operation(summary = "删除组件数据模型")
	@Parameters({ @Parameter(name = "componentDataModelId", description = "组件数据模型id", required = true) })
	@DeleteMapping("/{componentDataModelId}")
	@Tx
	public R<String> deleteUserById(@PathVariable("componentDataModelId") String componentDataModelId) {
		try {
			if (ComponentDataModel.dao.deleteById(componentDataModelId)) {
				return R.ok("删除成功.");
			} else {
				return R.fail("删除失败.");
			}
		} catch (Exception e) {
			log.error("删除组件数据模型异常:{}", e.getMessage());
			return R.fail(e.getMessage());
		}
	}
	
	@Operation(summary = "复制组件数据模型")
	@Parameters({ @Parameter(name = "componentDataModelId", description = "组件数据模型Id", required = true) })
	@GetMapping("/{componentDataModelId}/copy")
	public R<ComponentDataModel> copy(@PathVariable("componentDataModelId") String componentDataModelId) {
		try {
			ComponentDataModel componentDataModel=ComponentDataModel.dao.findById(componentDataModelId);
			if(componentDataModel!=null) {
				componentDataModel.setId(UuidUtil.getUUID32());
				componentDataModel.setCreateUserId(SecurityUtils.getUserId());
				componentDataModel.setCreateTime(new Date(System.currentTimeMillis()));
				if(componentDataModel.save()) {
					return R.ok(componentDataModel);
				}
			}
			return R.fail("复制组件数据模型失败.");
		} catch (Exception e) {
			return R.fail(e.getMessage());
		}
	}

	@Operation(summary = "获取组件对应的数据模型列表")
	@Parameters({ @Parameter(name = "componentId", description = "组件数据模型Id", required = true),@Parameter(name = "type", description = "组件数据模型类型", required = true) })
	@GetMapping("/{componentId}/{type}")
	public R<List<ComponentDataModel>> getComponentDataModels(@PathVariable("componentId") String componentId,@PathVariable("type") String type) {
		try {
			List<ComponentDataModel> componentDataModels=ComponentDataModel.dao.find("select * from "+ComponentDataModel.TABLE_NAME+" where component_id=? and type = ?",componentId,type);
			return R.ok(componentDataModels);
		} catch (Exception e) {
			return R.fail(e.getMessage());
		}
	}
	
}
